﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProjetoBe.cs;
using System.Data.SqlClient;

namespace ProjetoDalc.cs
{
    public class IngressoDalc
    {
        public void cadastarIngresso(IngressoBe objeto)
        {
            try
            {
                using (SqlConnection conexao = Connection.CriarConexao())
                {
                    SqlCommand command = new SqlCommand();
                    SqlDataAdapter adapter = new SqlDataAdapter();

                    command.Connection = conexao;
                    command.CommandText = "INSERT INTO INGRESSO VALUES(@CRIANCA_ID, @RESPOSANVEL_ID, @VALOR, @DATA, @DESCONTO, @DEPENDENCIA);";
                    command.Parameters.AddWithValue("CRIANCA_ID", objeto.idCrianca);
                    command.Parameters.AddWithValue("RESPOSANVEL_ID", objeto.idResponsavel);
                    command.Parameters.AddWithValue("VALOR", objeto.valor);
                    command.Parameters.AddWithValue("DATA", objeto.dataDeCompra);
                    command.Parameters.AddWithValue("DESCONTO", objeto.desconto);
                    command.Parameters.AddWithValue("DEPENDENCIA", objeto.dependecia);

                    adapter.InsertCommand = command;
                    conexao.Open();
                    command.ExecuteNonQuery();
                    conexao.Close();
                }
            }
            catch{}
        }

        public IngressoBe ultimoIngresso()
        {
            IngressoBe ingresso = new IngressoBe();
            try
            {
                SqlDataReader reader;
                using (SqlConnection conexao = Connection.CriarConexao())
                {
                    SqlCommand command = new SqlCommand();
                    SqlDataAdapter adapter = new SqlDataAdapter();

                    command.Connection = conexao;
                    command.CommandText = "SELECT * FROM INGRESSO I WHERE I.INGRESSO_ID In (Select Max(INGRESSO_ID) From INGRESSO)";
                    conexao.Open();
                    reader = command.ExecuteReader();

                    if(reader.HasRows)
                    {
                        while(reader.Read())
                        {
                            ingresso.id = Convert.ToInt32(reader["INGRESSO_ID"]);
                            ingresso.idCrianca = Convert.ToInt32(reader["CRIANCA_ID"]);
                            ingresso.idResponsavel = Convert.ToInt32(reader["RESPONSAVEL_ID"]);
                            ingresso.valor = reader["INGRESSO_VALOR"].ToString();
                            ingresso.dataDeCompra = Convert.ToDateTime(reader["INGRESSO_DATA"]);
                            ingresso.desconto = reader["INGRESSO_DESCONTO"].ToString();
                            ingresso.dependecia = Convert.ToInt32(reader["NUMERO_DEPENDENCIA"]);
                        }
                    }
                    conexao.Close();
                }
            }
            catch{}

            return ingresso;
        }
    }
}
